// pages/integration/integration.js
var common = require("../../../common/common.js").common;

const {$Toast: $Toast} = require("../../../dist/base/index");

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        tabData: [ {
            id: "-1",
            name: "全部商品"
        } ],
        tabCurrent: "",
        loading: false,
        loadingTip: "上拉加载更多",
        loadingStatus: false,
        page: 1,
        proList: [],
        pageStopButt: false
    },
    //标签切换
    tabChange(e) {
        //console.log(e.currentTarget.dataset,e.target.dataset)
        var that = this;
        var currentKey = e.target.dataset.key;
        //获取点击元素的key属性值
        //页码、产品列表清空
                that.setData({
            tabCurrent: currentKey,
            page: 1,
            proList: [],
            pageStopButt: false
        });
        that.getProList(currentKey, 1);
    },
    //获取用户信息
    getUsersInfomation: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                console.log(res);
                that.setData({
                    info: res.extra
                });
            }
        });
    },
    //查看商品详情
    viewProDetail(e) {
        wx.navigateTo({
            url: "/integrationPackage/pages/exchangeDetail/exchangeDetail?pid=" + e.currentTarget.dataset.id
        });
    },
    //立即兑换
    exchangeButt(e) {
        let sku_item = e.currentTarget.dataset.id + "," + "1";
        wx.navigateTo({
            url: "/integrationPackage/pages/exchange_order/exchange_order?sku_item=" + sku_item + "&status=0"
        });
    },
    //兑换红包
    exchangeRedBag(e) {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        //判断积分是否足够
                if (parseFloat(that.data.redBagList.all_integral) >= parseFloat(e.currentTarget.dataset.integration)) {
            common.request({
                endpoint: "Integral/exchange/voucher",
                method: "post",
                data: {
                    openid: openid,
                    gid: e.currentTarget.dataset.id
                },
                success: res => {
                    //console.log(res, "exchangeRedbag")
                    if (res.code == 0) {
                        $Toast({
                            content: res.message
                        });
                        //刷新总积分
                                                that.getRedBag();
                    } else {
                        $Toast({
                            content: res.msg
                        });
                    }
                }
            });
        } else {
            $Toast({
                content: "积分不足"
            });
        }
    },
    //获取分类导航
    getNavClass() {
        //
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Integral/integ_good/catalog",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                console.log(res, "分类");
                var tabData = that.data.tabData;
                res.extra.forEach((item, index) => {
                    tabData.push(item);
                });
                that.setData({
                    tabData: tabData,
                    tabCurrent: tabData[0].id
                });
                that.getProList(tabData[0].id, 1);
            }
        });
    },
    //获取分类商品
    getProList(pid, page) {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        //请求前,底部提示变为加载中
                that.setData({
            loadingTip: "",
            loadingStatus: true
        });
        common.request({
            endpoint: "Integral/integ_good/show",
            method: "post",
            data: {
                openid: openid,
                pid: pid,
                page: page
            },
            success: res => {
                //请求成功,底部提示变为提示
                //做页数判断
                //console.log(res, "商品");
                //增加产品
                var proList = that.data.proList;
                res.extra.forEach((item, index) => {
                    proList.push(item);
                });
                if (res.extra.length < 10) {
                    //未满一页,禁止再刷新
                    that.setData({
                        proList: proList,
                        pageStopButt: true,
                        loadingTip: "没有更多数据",
                        loadingStatus: false
                    });
                } else {
                    that.setData({
                        proList: proList,
                        loadingTip: "上拉加载更多",
                        loadingStatus: false
                    });
                }
            }
        });
    },
    //获取红包
    getRedBag() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Integral/integ_good/show_vou",
            method: "post",
            data: {
                openid: openid,
                page: 1
            },
            success: res => {
                console.log(res, "hongb");
                var data = res.extra;
                data.pro.forEach((item, index) => {
                    var intNum = parseInt(item.discount);
                    var floatNum = item.discount - intNum == 0 ? [ "0", "00" ] : (item.discount - intNum).toFixed(2).toString().split(".");
                    item.intNum = intNum;
                    item.floatNum = floatNum[1];
                    if (item.name.length > 5) {
                        item.name = item.name.substring(0, 5);
                    }
                    //console.log(item.name);
                                });
                that.setData({
                    redBagList: data
                });
            }
        });
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        var that = this;
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        that.getUsersInfomation();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        var that = this;
        //初始化类目、商品列表、红包列表
                that.setData({
            tabData: [ {
                id: "-1",
                name: "全部商品"
            } ],
            redBagList: [],
            proList: [],
            pageStopButt: false,
            page: 1
        });
        that.getNavClass();
        that.getRedBag();
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        var that = this;
        //获取页码、当前分类
                var page = that.data.page;
        var classes = that.data.tabCurrent;
        if (!that.data.pageStopButt) {
            //没有禁止刷新
            that.getProList(classes, page + 1);
            that.setData({
                page: page + 1
            });
        }
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {}
});